<?php

namespace app\api\controller;

use app\admin\model\BrowseLog;
use app\admin\model\BrowseSiteLog;
use app\admin\model\CollectSite;
use app\api\validate\RoomCheck;
use app\common\controller\Api;
use app\common\library\Ems;
use app\common\library\Sms;
use fast\Random;
use think\Db;
use think\Validate;

/**
 * 会员接口
 */
class User extends Api
{
    protected $noNeedLogin = ['login', 'mobilelogin', 'register', 'site_register' ,'resetpwd', 'changeemail', 'changemobile', 'third','siteInfo','checkUsername'];
    protected $noNeedRight = '*';
    protected $noNeedSite  = '*';
    protected $noNeedCheck = ['*'];

    public function _initialize()
    {
        parent::_initialize();
    }

    /**
     * 会员中心
     */
    public function index()
    {
        $this->success('', ['welcome' => $this->auth->nickname]);
    }

    /**
     * 会员登录
     *
     * @param string $account  账号
     * @param string $password 密码
     */
    public function login()
    {
        $account = $this->request->request('account');
        $password = $this->request->request('password');
        if (!$account || !$password) {
            $this->error(__('Invalid parameters'));
        }
        $ret = $this->auth->login($account, $password);
        if ($ret) {
            $data = ['userinfo' => $this->auth->getUserinfo()];
            $this->success(__('Logged in successful'), $data);
        } else {
            $this->error($this->auth->getError());
        }
    }

    /**
     * 手机验证码登录
     *
     * @param string $mobile  手机号
     * @param string $captcha 验证码
     */
    public function mobilelogin()
    {
        $mobile = $this->request->request('mobile');
        $captcha = $this->request->request('captcha');
        if (!$mobile || !$captcha) {
            $this->error(__('Invalid parameters'));
        }
        if (!Validate::regex($mobile, "^1\d{10}$")) {
            $this->error(__('Mobile is incorrect'));
        }
        if (!Sms::check($mobile, $captcha, 'mobilelogin')) {
            $this->error(__('Captcha is incorrect'));
        }
        $user = \app\common\model\User::getByMobile($mobile);
        if ($user) {
            //如果已经有账号则直接登录
            $ret = $this->auth->direct($user->id);
        } else {
            $ret = $this->auth->register($mobile, Random::alnum(), '', $mobile, []);
        }
        if ($ret) {
            Sms::flush($mobile, 'mobilelogin');
            $data = ['userinfo' => $this->auth->getUserinfo()];
            $this->success(__('Logged in successful'), $data);
        } else {
            $this->error($this->auth->getError());
        }
    }

    /**
     * 注册会员
     *
     * @param string $username 用户名
     * @param string $password 密码
     * @param string $email    邮箱
     * @param string $mobile   手机号
     */
    public function register()
    {
        $username = $this->request->request('username');
        $password = $this->request->request('password');
        $email = $this->request->request('email');
        $mobile = $this->request->request('mobile');
        $param = $this->request->request();
        $validate = new \app\api\validate\User();
        if (!$validate->scene('user_add')->check($param)) {
            $this->error($validate->getError());
        }
        $extra = [];
        if(!empty($param['group'])){
            $extra['group_id'] = $param['group'];
        }
        if(!empty($param['gender'])){
            $extra['gender'] = $param['gender'];
        }
        if(!empty($param['qq'])){
            $extra['qq'] = $param['qq'];
        }
        if(!empty($param['wechat'])){
            $extra['wechat'] = $param['wechat'];
        }
        if(!empty($param['dingding'])){
            $extra['dingding'] = $param['dingding'];
        }
        if(!empty($param['email']) && !Validate::is($param['email'], "email")){
            $extra['email'] = $param['email'];
        }
        if (!$username || !$password) {
            $this->error(__('Invalid parameters'));
        }
        if ($mobile && !Validate::regex($mobile, "^1\d{10}$")) {
            $this->error(__('Mobile is incorrect'));
        }
        $ret = $this->auth->register($username, $password, $email, $mobile, $extra);
        if ($ret) {
            $data = ['userinfo' => $this->auth->getUserinfo()];
            $this->success(__('Sign up successful'), $data);
        } else {
            $this->error($this->auth->getError());
        }
    }

    /**
     * Notes: 站长注册
     * User: hawk
     * Email: 2271030162@qq.com
     * Date: 2019/6/20 0020
     * Time: 15:38
     */
    public function site_register()
    {
        $username = $this->request->request('username');
        $password = $this->request->request('password');
        $email = $this->request->request('email');
        $mobile = $this->request->request('mobile');
        $param = $this->request->param();
        $extra = [];
        $extra['group_id'] = 7;
        $validate = new \app\api\validate\User();
        if (!$validate->scene('site_add')->check($param)) {
            $this->error($validate->getError());
        }
        if ($mobile && !Validate::regex($mobile, "^1\d{10}$")) {
            $this->error(__('Mobile is incorrect'));
        }
        if(!empty($param['phone']) && $mobile==$param['phone']){
            $this->error('紧急电话不可和注册电话一样');
        }
        if(!empty($param['qq'])){
            $extra['qq'] = $param['qq'];
        }
        if(!empty($param['wechat'])){
            $extra['wechat'] = $param['wechat'];
        }
        if(!empty($param['dingding'])){
            $extra['dingding'] = $param['dingding'];
        }
        if(!empty($param['email']) && !Validate::is($param['email'], "email")){
            $extra['email'] = $param['email'];
        }
        if(!isset($param['sitename']) || empty($param['sitename']))$this->error('请填写站点名称');
        if(!isset($param['site_level']) || !in_array($param['site_level'],[1,2,3,4,5]) || empty($param['site_area_id']))$this->error('请选择有效站点');
        $extra['sitename'] = $param['sitename'];
        $extra['site_level'] = $param['site_level'];
        $extra['site_area_id'] = $param['site_area_id'];
        $issetSite = issetSite($param['site_area_id'],$param['site_level']);
        if($issetSite)$this->error('该行政区域站点已建立');
        $areaArr = getAreaLevel($param['site_area_id'],$param['site_level']);
        if($areaArr){
            $extra = array_merge($extra,$areaArr);
        }
        if($extra['site_level']==5){
            $extra['site_type'] = '2';
        }elseif ($extra['site_level']<5 && $extra['site_level']>0){
            $extra['site_type'] = '1';
        }
        $extra['area_str'] = Db::name('area2')->where('id',$param['site_area_id'])->value('whole_name');
        $extra['phone'] = !empty($param['phone'])?$param['phone']:'';
        $extra['is_siter'] = 1;
        if (!$username || !$password) {
            $this->error(__('Invalid parameters'));
        }
        $ret = $this->auth->register($username, $password, $email, $mobile, $extra);
        if ($ret) {
            $data = ['userinfo' => $this->auth->getUserinfo()];
            $this->success(__('Sign up successful'), $data);
        } else {
            $this->error($this->auth->getError());
        }
    }

    /**
     * Notes: 校核账号
     * User: hawk
     * Email: 2271030162@qq.com
     * Date: 2019/8/19 0019
     * Time: 9:33
     */
    public function checkUsername(){
        $username = $this->request->request('username');
        $info = Db::name('user')->where('username',$username)->find();
        if(empty($info))$this->success('可以使用');
        $this->error('此账号已被使用');
    }

    /**
     * 注销登录
     */
    public function logout()
    {
        $this->auth->logout();
        $this->success(__('Logout successful'));
    }

    /**
     * 修改会员个人信息
     *
     * @param string $avatar   头像地址
     * @param string $username 用户名
     * @param string $nickname 昵称
     * @param string $bio      个人简介
     */
    public function profile()
    {
        $user = $this->auth->getUser();
        $username = $this->request->request('username');
        $nickname = $this->request->request('nickname');
        $avatar = $this->request->request('avatar', '', 'trim,strip_tags,htmlspecialchars');
        if ($username) {
            $exists = \app\common\model\User::where('username', $username)->where('id', '<>', $this->auth->id)->find();
            if ($exists) {
                $this->error(__('Username already exists'));
            }
            $user->username = $username;
        }
        if($nickname){
            $user->nickname = $nickname;
        }
        if($avatar){
            $user->avatar = $avatar;
        }
        $user->save();
        $this->success();
    }

    /**
     * 修改邮箱
     *
     * @param string $email   邮箱
     * @param string $captcha 验证码
     */
    public function changeemail()
    {
        $user = $this->auth->getUser();
        $email = $this->request->post('email');
        $captcha = $this->request->request('captcha');
        if (!$email || !$captcha) {
            $this->error(__('Invalid parameters'));
        }
        if (!Validate::is($email, "email")) {
            $this->error(__('Email is incorrect'));
        }
        if (\app\common\model\User::where('email', $email)->where('id', '<>', $user->id)->find()) {
            $this->error(__('Email already exists'));
        }
        $result = Ems::check($email, $captcha, 'changeemail');
        if (!$result) {
            $this->error(__('Captcha is incorrect'));
        }
        $verification = $user->verification;
        $verification->email = 1;
        $user->verification = $verification;
        $user->email = $email;
        $user->save();

        Ems::flush($email, 'changeemail');
        $this->success();
    }

    /**
     * 修改手机号
     *
     * @param string $email   手机号
     * @param string $captcha 验证码
     */
    public function changemobile()
    {
        $user = $this->auth->getUser();
        $mobile = $this->request->request('mobile');
        $newmobile = $this->request->request('newmobile');
        $captcha = $this->request->request('captcha');
        if (!$mobile || !$captcha) {
            $this->error(__('Invalid parameters'));
        }
        if (!Validate::regex($mobile, "^1\d{10}$")) {
            $this->error(__('Mobile is incorrect'));
        }
        if (!Validate::regex($newmobile, "^1\d{10}$")) {
            $this->error(__('Mobile is incorrect'));
        }
        if($user->mobile!=$mobile)$this->error('原手机号不正确');
        $result = Sms::check($mobile, $captcha, 'changemobile');
        if (!$result) {
            $this->error(__('Captcha is incorrect'));
        }
        $verification = $user->verification;
        $verification->mobile = 1;
        $user->verification = $verification;
        $user->mobile = $newmobile;
        $user->save();
        Sms::flush($mobile, 'changemobile');
        $this->success();
    }

    /**
     * Notes: 校核用户电话号码
     * User: hawk
     * Email: 2271030162@qq.com
     * Date: 2019/8/7 0007
     * Time: 11:20
     */
    public function checkmobile(){
        $user = $this->auth->getUser();
        $mobile = $this->request->request('mobile');
        if($user->mobile!=$mobile)$this->error('原手机号不正确');
        $this->success('成功');
    }

    /**
     * 第三方登录
     *
     * @param string $platform 平台名称
     * @param string $code     Code码
     */
    public function third()
    {
        $url = url('user/index');
        $platform = $this->request->request("platform");
        $code = $this->request->request("code");
        $config = get_addon_config('third');
        if (!$config || !isset($config[$platform])) {
            $this->error(__('Invalid parameters'));
        }
        $app = new \addons\third\library\Application($config);
        //通过code换access_token和绑定会员
        $result = $app->{$platform}->getUserInfo(['code' => $code]);
        if ($result) {
            $loginret = \addons\third\library\Service::connect($platform, $result);
            if ($loginret) {
                $data = [
                    'userinfo'  => $this->auth->getUserinfo(),
                    'thirdinfo' => $result
                ];
                $this->success(__('Logged in successful'), $data);
            }
        }
        $this->error(__('Operation failed'), $url);
    }

    /**
     * 重置密码
     *
     * @param string $mobile      手机号
     * @param string $newpassword 新密码
     * @param string $captcha     验证码
     */
    public function resetpwd()
    {
        $type = $this->request->request("type",'mobile');
        $mobile = $this->request->request("mobile");
        $email = $this->request->request("email");
        $newpassword = $this->request->request("newpassword");
        $captcha = $this->request->request("captcha");
        if (!$newpassword || !$captcha) {
            $this->error(__('Invalid parameters'));
        }
        if ($type == 'mobile') {
            if (!Validate::regex($mobile, "^1\d{10}$")) {
                $this->error(__('Mobile is incorrect'));
            }
            $user = \app\common\model\User::getByMobile($mobile);
            if (!$user) {
                $this->error(__('User not found'));
            }
            $ret = Sms::check($mobile, $captcha, 'resetpwd');
            if (!$ret) {
                $this->error(__('Captcha is incorrect'));
            }
            Sms::flush($mobile, 'resetpwd');
        } else {
            if (!Validate::is($email, "email")) {
                $this->error(__('Email is incorrect'));
            }
            $user = \app\common\model\User::getByEmail($email);
            if (!$user) {
                $this->error(__('User not found'));
            }
            $ret = Ems::check($email, $captcha, 'resetpwd');
            if (!$ret) {
                $this->error(__('Captcha is incorrect'));
            }
            Ems::flush($email, 'resetpwd');
        }
        //模拟一次登录
        $this->auth->direct($user->id);
        $ret = $this->auth->changepwd($newpassword, '', true);
        if ($ret) {
            $this->success(__('Reset password successful'));
        } else {
            $this->error($this->auth->getError());
        }
    }

    /**
     * 重置密码
     *
     * @param string $mobile      手机号
     * @param string $newpassword 新密码
     * @param string $captcha     验证码
     */
    public function resetpwds()
    {
        $user_id = $this->user_id;;
        $newpassword = $this->request->request("newpassword");
        $oldpassword = $this->request->request("oldpassword");
        if (!$newpassword || !$oldpassword) {
            $this->error(__('Invalid parameters'));
        }
        //模拟一次登录
        $this->auth->direct($user_id);
        $ret = $this->auth->changepwd($newpassword, $oldpassword);
        if ($ret) {
            $this->success(__('Reset password successful'));
        } else {
            $this->error($this->auth->getError());
        }
    }

    /**
     * Notes: 用户房产认证列表
     * User: hawk
     * Email: 2271030162@qq.com
     * Date: 2019/6/19 0019
     * Time: 16:45
     * @throws \think\exception\DbException
     */
    public function roomCheckList(){
        $param = $this->request->request();
        $limit = input('limit',15);
        $where['rc.user_id'] = $param['user_id'];
        $where['rc.typedata'] = 1;
        $roomCheckModel = new \app\admin\model\RoomCheck();
        $list = $roomCheckModel
            ->field('rc.*,u.username,u.nickname')
            ->alias('rc')
            ->join('user u','u.id=rc.user_id')
            ->where($where)
            ->order('rc.createtime desc')
            ->paginate($limit);
        if($list){
            $this->success('成功',$list);
        }else{
            $this->error('无数据');
        }
    }

    /**
     * Notes: 房产认证提交
     * User: hawk
     * Email: 2271030162@qq.com
     * Date: 2019/6/19 0019
     * Time: 9:31
     */
    public function roomCheck(){
        $param = $this->request->request();
        if (isset($param['tel']) && !empty($param['tel']) && !Validate::regex($param['tel'], "^1\d{10}$")) {
            $this->error(__('Mobile is incorrect'));
        }
        $validate = new RoomCheck();
        if (!$validate->scene('add')->check($param)) {
            $this->error($validate->getError());
        }
        $roomCheckModel = new \app\admin\model\RoomCheck();
        if($roomCheckModel->_add($param))$this->success('操作成功');
        $this->error($roomCheckModel->getError());
    }

    /**
     * Notes: 认证未通过，编辑
     * User: hawk
     * Email: 2271030162@qq.com
     * Date: 2019/6/19 0019
     * Time: 17:25
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function editRoomCheck(){
        $param = $this->request->request();
        $validate = new RoomCheck();
        if (!$validate->scene('edit')->check($param)) {
            $this->error($validate->getError());
        }
        $roomCheckModel = new \app\admin\model\RoomCheck();
        if($roomCheckModel->_edit($param))$this->success('操作成功');
        $this->error($roomCheckModel->getError());
    }

    /**
     * Notes: 认证删除
     * User: hawk
     * Email: 2271030162@qq.com
     * Date: 2019/6/19 0019
     * Time: 17:38
     */
    public function deleteRoomCheck(){
        $param = $this->request->request();
        if(!isset($param['id']) || empty($param['id'])){
            $this->error('认证ID不可为空');
        }
        $roomCheckModel = new \app\admin\model\RoomCheck();
        if($roomCheckModel->_delete($param['id'],$param['user_id']))$this->success('操作成功');
        $this->error($roomCheckModel->getError());
    }

    /**
     * Notes: 站长认证提交
     * User: hawk
     * Email: 2271030162@qq.com
     * Date: 2019/6/20 0020
     * Time: 16:37
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function siteCheck(){
        $param = $this->request->request();
        $validate = new RoomCheck();
        if (!$validate->scene('siteCheck')->check($param)) {
            $this->error($validate->getError());
        }
        $roomCheckModel = new \app\admin\model\RoomCheck();
        $roomCheckNumber = $roomCheckModel->where('user_id',$param['user_id'])->where('status','2')->where('typedata','2')->find();
        if($roomCheckNumber)$this->error('已认证请勿重复提交');
        $param['typedata'] = '2';
        $userInfo = Db::name('user')->where('id',$param['user_id'])->find();
        $param['site_id'] = $param['user_id'];
        $param['level'] = $userInfo['site_level'];
        $param['province_id'] = $userInfo['province_id'];
        $param['city_id'] = $userInfo['city_id'];
        $param['area_id'] = $userInfo['area_id'];
        $param['town_id'] = $userInfo['town_id'];
        $param['area_str'] = $userInfo['area_str'];
        $param['site_area_id'] = $userInfo['site_area_id'];
        if($roomCheckModel->allowField(true)->save($param)){
            Db::name('user')->where('id',$param['user_id'])->update(['is_check'=>'2','checkdata'=>0]);
            $this->success('操作成功');
        }
        $this->error($roomCheckModel->getError());
    }

    /**
     * Notes: 用户房产认证检查读取身份信息
     * User: hawk
     * Email: 2271030162@qq.com
     * Date: 2019/6/25 0025
     * Time: 17:49
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function isRoomCheck(){
        $user_id = $this->user_id;;
        $where['user_id']=$user_id;
        $where['checkdata'] = '4';
        $where['typedata'] = '1';
        $where['status'] = '2';
        $roomCheck = new \app\admin\model\RoomCheck();
        $res = $roomCheck->field('card_number,cardzimage,cardfimage')->where($where)->find();
        if($res)$this->success('有认证',$res);
        $this->error('无认证');
    }

    /**
     * Notes: 用户信息
     * User: hawk
     * Email: 2271030162@qq.com
     * Date: 2019/7/9 0009
     * Time: 14:19
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function info(){
        $user_id = $this->user_id;
        $user = new \app\admin\model\User();
        $userInfo = $user::field('id as user_id,username,nickname,mobile,avatar,score,birthday,gender,prevtime,logintime,jointime,checkdata,status,is_siter,is_check,site_level,area_str,site_number')
            ->where('id',$user_id)
            ->find();
        if($userInfo->is_check==0){
            $userInfo->check = 3;
        }else{
            $userInfo->check = $userInfo->checkdata;
        }
        $this->success('成功！',$userInfo);
    }

    /**
     * Notes: 站点信息
     * User: hawk
     * Email: 2271030162@qq.com
     * Date: 2019/8/6 0006
     * Time: 16:44
     */
    public function siteInfo(){
        $site_id = input('site_id');
        if(empty($site_id))$this->error('缺少参数');
        $res = Db::name('user')->field('id as site_id,sitename,site_level,site_number')->where('id',$site_id)->where('is_siter',1)->find();
        if(empty($res))$this->error('无此站点');
        $this->success('',$res);
    }

    /**
     * Notes: 认证详情
     * User: hawk
     * Email: 2271030162@qq.com
     * Date: 2019/7/10 0010
     * Time: 11:45
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function checkDetail(){
        $check_id = input('id');
        if(empty($check_id))$this->error('缺少参数');
        $roomCheckModel = new \app\admin\model\RoomCheck();
        $detail = $roomCheckModel
            ->field('rc.*,u.mobile')
            ->alias('rc')
            ->join('user u','u.id=rc.user_id')
            ->where('rc.id',$check_id)
            ->find();
        if($detail)$this->success('成功',$detail);
        $this->error('失败');
    }

    /**
     * Notes: 我的提问
     * User: hawk
     * Email: 2271030162@qq.com
     * Date: 2019/7/18 0018
     * Time: 10:50
     */
    public function myAskingList(){
        $user_id = $this->user_id;;
        $limit = input('limit');
        $order = "createtime desc";
        $where=[];
        if(!empty($user_id)){
            $where['user_id'] = $user_id;
        }
        $where['status'] = '1';
        $field="";
        $askingModel = new \app\common\model\Asking();
        $askingData = $askingModel->_list($field,$where,$limit,$order);
        $this->success('成功',$askingData);
    }

    /**
     * Notes: 我的文章
     * User: hawk
     * Email: 2271030162@qq.com
     * Date: 2019/6/26 0026
     * Time: 15:58
     */
    public function articleList(){
        $user_id = $this->user_id;;
        $limit = input('limit');
        $order = "createtime desc";
        $where['user_id'] = $user_id;
        $where['status'] = '1';
        $field="*";
        $askingModel = new \app\common\model\Article();
        $askingData = $askingModel->_list($field,$where,$limit,$order);
        $this->success('成功',$askingData);
    }

    /**
     * Notes: 已认证的站点
     * User: hawk
     * Email: 2271030162@qq.com
     * Date: 2019/7/20 0020
     * Time: 15:53
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function userSite(){
        $user_id = $this->user_id;
        $roomCheckModel = new \app\admin\model\RoomCheck();
        $where['rc.user_id'] = $user_id;
        $where['rc.status']= '2';
        $field = "rc.id,rc.site_id,u.sitename,rc.area_str,rc.level,rc.province_id,rc.city_id,rc.area_id,rc.town_id";
        $siteList = $roomCheckModel
            ->alias('rc')
            ->field($field)
            ->join('user u','u.id=rc.site_id')
            ->where($where)
            ->group('rc.site_id')
            ->select();
        if(!empty($siteList))$this->success('成功',$siteList);
        $this->error('失败');
    }

    /**
     * Notes: 收藏站点
     * User: hawk
     * Email: 2271030162@qq.com
     * Date: 2019/7/25 0025
     * Time: 10:52
     */
    public function collectSite(){
        $site_id = input('site_id');
        $user_id = $this->user_id;;
        if(empty($site_id) || empty($user_id)){
            $this->error('缺少参数');
        }
        $siteInfo = Db::name('user')->where('id',$site_id)->find();
        if(!$siteInfo)$this->error('无此站点');
        $data = [
            'name'=>$siteInfo['sitename'],
            'site_id'=>$site_id,
            'user_id'=>$user_id,
            'level'=>$siteInfo['site_level']
        ];
        $res = (new CollectSite())->save($data);
        if($res)$this->success('成功');
        $this->error('失败');
    }

    public function collectSiteDelete(){
        $collect_id = input('collect_id');
        if(empty($collect_id)){
            $this->error('缺少参数');
        }
        $res = (new CollectSite())->where('id',$collect_id)->delete();
        if($res)$this->success('成功');
        $this->error('失败');
    }

    /**
     * Notes: 收藏站点列表
     * User: hawk
     * Email: 2271030162@qq.com
     * Date: 2019/7/25 0025
     * Time: 10:54
     * @throws \think\exception\DbException
     */
    public function collectSiteList(){
        $user_id = $this->user_id;;
        $limit = input('limit',10);

        $where['b.user_id']=$user_id;
        $res = Db::name('collect_site')
            ->field('b.id,u.sitename,b.site_id,u.site_level,b.user_id,u.site_number')
            ->alias('b')
            ->join('user u','u.id=b.site_id')
            ->where($where)
            ->order('b.createtime desc')
            ->paginate($limit);
        if($res)$this->success('成功',$res);
        $this->error('失败');
    }

    /**
     * Notes: 记录访问记录
     * User: hawk
     * Email: 2271030162@qq.com
     * Date: 2019/8/6 0006
     * Time: 15:28
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function browseSiteLog(){
        $user_id = $this->user_id;;
        $site_id = input('site_id');
        $siteInfo = (new \app\common\model\User())->where('id',$site_id)->find();
        if(empty($siteInfo))$this->error('无此站点');
        $browseSiteLog = (new BrowseSiteLog())->where('user_id',$user_id)->where('site_id',$site_id)->find();
        if($browseSiteLog){
            $browseSiteLog->updatetime = time();
            $ret = $browseSiteLog->save();
        }else{
            $data =['user_id'=>$user_id,'site_id'=>$site_id,'updatetime'=>time()];
            $res = (new BrowseSiteLog())->save($data);
        }
        if($res)$this->success();
        $this->error();
    }

    /**
     * Notes: 访问记录列表
     * User: hawk
     * Email: 2271030162@qq.com
     * Date: 2019/8/6 0006
     * Time: 15:29
     */
    public function browseSiteLogList(){
        $user_id = $this->user_id;;
        $limit = input('limit',15);
        $where['b.user_id']=$user_id;
        $ret = Db::name('browse_site_log')
            ->field('b.id,u.sitename,b.site_id,u.site_level,b.user_id,u.site_number')
            ->alias('b')
            ->join('user u','u.id=b.site_id')
            ->where($where)
            ->order('b.updatetime desc')
            ->paginate($limit);
        if($ret)$this->success('成功',$ret);
        $this->error('失败');
    }

    public function deleteBrowseSiteLog(){
        $id = input('id',0);
        if($id){
            $where['id'] = $id;
        }
        $user_id = $this->user_id;
        $where['user_id']=$user_id;
        $ret = (new BrowseSiteLog())->where($where)->delete();
        if($ret)$this->success('成功',$ret);
        $this->error('失败');
    }

}
